package com.xing.servlet;

import com.xing.domain.Book;
import com.xing.service.BookService;
import com.xing.service.impl.BookServiceImpl;
import com.xing.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day41bookshop
 * 2021/5/6 17:55
 */
@WebServlet(value = "/updateui")
public class UpdateUIServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if(StringUtils.isEmptry(id)){
            req.setAttribute("msg", "书籍ID不能为空");
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
            return;
        }
        //把传过来的id变成整数类型
        try {
            int boolId = Integer.parseInt(id);
            BookService bookService = new BookServiceImpl();
            Book book=  bookService.findById(boolId);
            if(book!=null){
                req.setAttribute("book", book);
                req.getRequestDispatcher("/update.jsp").forward(req,resp);
            }
            req.setAttribute("msg", "书籍不存在");
            req.getRequestDispatcher("/message.jsp").forward(req,resp);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
